{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "907d2182",
   "metadata": {},
   "source": [
    "## API文档介绍\n",
    "### 主要学习内容：图像识别(认知服务)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b13d67d",
   "metadata": {},
   "source": [
    "# 百度AI开放平台\n",
    "### 通用物体和场景识别\n",
    "* 接口描述\n",
    ">该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "\n",
    "* 请求示例\n",
    ">HTTP 方法：POST\n",
    "\n",
    "* 请求URL\n",
    ">https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\n",
    "\n",
    "### 鉴权认证机制(access_token)\n",
    "* 获取Access Token\n",
    "* 获取access_token示例代码:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "372722c7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T12:17:53.381137Z",
     "start_time": "2022-04-04T12:17:52.603722Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.2c5b9dbd6c9bc3c2c7bfa43ea7f9e84d.315360000.1964434673.282335-25904912', 'expires_in': 2592000, 'session_key': '9mzdAvbkpFLsvzkznmJ/K3Qm/hJPWXOEuxU+lp9ivJDVSwGMYgrhyJTl+YEeoL+HIaKXAEpfvyn5cRhTydj6wcSJ9Z4NGw==', 'access_token': '24.942dfd2749a9b7519efd166ae36edf78.2592000.1651666673.282335-25904912', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'e977774d4f57691ade4a908a9fdf8994'}\n"
     ]
    }
   ],
   "source": [
    "# 方1\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=BRuhUVsPKZx92IiWlDF93F2m&client_secret=0SvKOPtyfiYgQ1trP5is5QXYXrxFYuGM'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c707c1d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T12:22:07.103096Z",
     "start_time": "2022-04-04T12:22:07.090991Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.942dfd2749a9b7519efd166ae36edf78.2592000.1651666673.282335-25904912'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "29a55da8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T12:17:24.419621Z",
     "start_time": "2022-04-04T12:17:24.004368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.d698f49c05626dfbedb16b09c46cb85d.315360000.1964434645.282335-25904912', 'expires_in': 2592000, 'session_key': '9mzdX7uWbNdfpCkTdfs7mC1z8QeVHLzQC8v9PjaA8twiXEc1a3aXNc2xbcdIb+oTTHusXQ3HBJU/rOG2VR16M3z75aRfzg==', 'access_token': '24.da4b820899f670fc0d2e7740581fffda.2592000.1651666645.282335-25904912', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '65aaee07f9cab95c9132ec241129ef38'}\n"
     ]
    }
   ],
   "source": [
    "# 方2\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'BRuhUVsPKZx92IiWlDF93F2m',\n",
    "    'client_secret':'0SvKOPtyfiYgQ1trP5is5QXYXrxFYuGM'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7c91943",
   "metadata": {},
   "source": [
    "## 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "46c82455",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:56:52.807679Z",
     "start_time": "2022-04-04T11:56:52.308155Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "75eabb72",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T12:28:58.815621Z",
     "start_time": "2022-04-04T12:28:57.700701Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '樱兰高校', 'score': 0.888851, 'root': '非自然图像-彩色动漫'}, {'keyword': '卡通动漫人物', 'score': 0.654852, 'root': '非自然图像-彩色动漫'}, {'keyword': '手表', 'score': 0.375052, 'root': '商品-钟表'}, {'keyword': '图画', 'score': 0.192792, 'root': '商品-绘画'}, {'keyword': '工笔画', 'score': 0.009903, 'root': '商品-绘画'}], 'log_id': 1510957601598908341}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('xiamu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.942dfd2749a9b7519efd166ae36edf78.2592000.1651666673.282335-25904912'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d80a4fb3",
   "metadata": {},
   "source": [
    "# 作业：尝试使用百度API图像识别其中一个\n",
    "* 做用户需求分析并设计一个最小可行性应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00d8177a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
